package com.yanlight.app.controller;

import cn.jiguang.common.ClientConfig;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import com.alibaba.fastjson.JSONObject;
import com.yanlight.exception.ErrorType;
import com.yanlight.support.PropertiesHolder;
import com.yanlight.support.http.Response;
import com.yanlight.util.DateTimeUtil;
import com.yanlight.util.LogUtil;
import com.yanlight.util.ResponseUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/test")
public class TestController {

    private Integer orderId = 0;

    private String APP_KEY = PropertiesHolder.get("jpush.appKey");
    private String APP_SECRET = PropertiesHolder.get("jpush.master.secret");


    @RequestMapping("/multiPush")
    @ResponseBody
    public Response multiPush(Integer companyId, Integer count) {

        PushPayload.Builder PayloadBuilder = PushPayload.newBuilder();
        PayloadBuilder.setPlatform(Platform.all());

        Audience audience = Audience.alias("dev.payment.notify." + companyId);
        PayloadBuilder.setAudience(audience);

        ClientConfig clientConfig = ClientConfig.getInstance();
        JPushClient client = new JPushClient(APP_SECRET, APP_KEY, null, clientConfig);

        if (count == null) {
            count = 200;
        }

        try {
            for (int i = 0; i < count; i++) {

                int random = (int) (Math.random() * (200 - 1) + 1);
                int payType = i % 2 + 1;

                String fee = random + "." + i;

                HashMap<String, String> messageEntity = new HashMap<>();
                messageEntity.put("fee", fee);
                messageEntity.put("orderId", (i + 1) + "");
                messageEntity.put("payTime", DateTimeUtil.getNowTime());
                messageEntity.put("payType", payType + "");
                String content = null;
                if (payType == 1) {
                    content = "支付宝收款" + fee + "元";
                } else if (payType == 2) {
                    content = "微信收款" + fee + "元";
                }
                messageEntity.put("text", content);

                LogUtil.showLog(messageEntity);


                Map<String, String> iosParam = new HashMap<String, String>();
                iosParam.put("content-available", "true");
                Notification notification = Notification.ios("新消息", iosParam);
                PayloadBuilder.setNotification(notification).setMessage(Message.newBuilder().setContentType("pay").setMsgContent(JSONObject.toJSONString(messageEntity)).build());
                PushPayload payload = PayloadBuilder.build();

                client.sendPush(payload);

                TimeUnit.MILLISECONDS.sleep(500);//毫秒
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseUtil.success();
    }

    @RequestMapping("/push")
    @ResponseBody
    public Response push(Integer companyId, Integer payType, Double fee) {
        PushResult result = null;

        orderId++;

        try {

            HashMap<String, String> messageEntity = new HashMap<>();
            messageEntity.put("fee", fee.toString());
            messageEntity.put("orderId", orderId.toString());
            messageEntity.put("payTime", DateTimeUtil.getNowTime());
            messageEntity.put("payType", payType.toString());
            String content = null;
            if (payType == 1) {
                content = "支付宝收款" + fee + "元";
            } else if (payType == 2) {
                content = "微信收款" + fee + "元";
            }
            messageEntity.put("text", content);

            PushPayload.Builder PayloadBuilder = PushPayload.newBuilder();
            PayloadBuilder.setPlatform(Platform.all());

            Audience audience = Audience.alias("dev.payment.notify." + companyId);
            PayloadBuilder.setAudience(audience);

            ClientConfig clientConfig = ClientConfig.getInstance();
            JPushClient client = new JPushClient(APP_SECRET, APP_KEY, null, clientConfig);


            Map<String, String> iosParam = new HashMap<String, String>();
            iosParam.put("content-available", "true");
            Notification notification = Notification.ios("新消息", iosParam);

            PayloadBuilder.setNotification(notification).setMessage(Message.newBuilder().setContentType("pay").setMsgContent(JSONObject.toJSONString(messageEntity)).build());
            PushPayload payload = PayloadBuilder.build();

            client.sendPush(payload);
        } catch (Exception e) {
            return ResponseUtil.fail(ErrorType.SYSTEM_ERROR);
        }
        return ResponseUtil.success();
    }

}
